package com.qxsx.midi.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.config.AppManager;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.util.Timer;
import java.util.TimerTask;

import java.util.logging.LogRecord;

/**
 * Created by Zrj on 2015/9/1.
 */
public class ForgetActivity extends BaseActivity implements View.OnClickListener {
    private Button btn_verify;//验证
    private TextView tv_init, tv_shouji, tv_yanzheng;//最上面的三对字
    private Button btn_send;//发送验证码
    private EditText et_phone, et_yanzheng;
    private final static int WHAT_DAOJISHI = 1;

    // 可以验证
    boolean canChecking = true;
    // 验证用的Timer
    Timer timer;
    // 间隔时间
    final int spacing = 60;
    // 剩余时间
    int lastTime;

    String yanzhengma = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        setTitle("找回密码");
        setNavBtn(R.mipmap.back, 0);
        findById();
        AppManager.getAppManager().addActivity(this);

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case WHAT_DAOJISHI:
                    if (canChecking)    // 如果可以获取验证码
                    {
                        canChecking = false;
                        timer = new Timer();
                        lastTime = spacing;
                        btn_send.setBackgroundResource(R.mipmap.yanzhengs);
                        final String oldtxt = btn_send.getText().toString();
                        btn_send.setText(lastTime + "S");
                        btn_send.setTextColor(getResources().getColor(R.color.theme_green));
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                if (lastTime == 0) // 时间到了
                                {
                                    ForgetActivity.this.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            btn_send.setClickable(true);
                                            btn_send.setBackgroundResource(R.mipmap.yanzheng);
                                            btn_send.setText(oldtxt);
                                            btn_send.setTextColor(getResources().getColor(R.color.theme_green));
                                            timer.cancel();
                                            timer = null;
                                            canChecking = true;
                                        }
                                    });

                                } else {

                                    ForgetActivity.this.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            btn_send.setClickable(false);
                                            lastTime--;
                                            btn_send.setText(lastTime + "S");
                                        }
                                    });

                                }
                            }
                        }, 1000, 1000);
                    } else            // 不可以获取验证码
                    {
                        return;
                    }


                    break;
            }
        }
    };

    private void findById() {

        tv_init = (TextView) findViewById(R.id.tv_init);
        tv_shouji = (TextView) findViewById(R.id.tv_shouji);
        tv_yanzheng = (TextView) findViewById(R.id.tv_yanzheng);


        et_phone = (EditText) findViewById(R.id.et_phone);
        et_phone.setInputType(EditorInfo.TYPE_CLASS_PHONE);


        et_yanzheng = (EditText) findViewById(R.id.et_yanzheng);


        btn_send = (Button) findViewById(R.id.btn_send);
        btn_verify = (Button) findViewById(R.id.btn_verify);


        btn_send.setOnClickListener(this);
        btn_verify.setOnClickListener(this);
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                AppManager.getAppManager().finishActivity(ForgetActivity.class);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null)
            timer.cancel();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_verify:
                if (TextUtils.isEmpty(et_phone.getText().toString()) || !Tools.isMobileNO(et_phone.getText().toString().trim())) {
                    ShowToast("请输入正确的手机号");
                } else {
                    if (TextUtils.isEmpty(et_yanzheng.getText().toString())) {
                        ShowToast("请您输入验证码");
                    } else {
                        tv_init.setTextColor(getResources().getColor(R.color.text_black));
                        tv_shouji.setTextColor(getResources().getColor(R.color.text_black));
                        tv_yanzheng.setTextColor(getResources().getColor(R.color.theme_green));
                        /**
                         * 在这里判断验证码是否正确
                         */
                        if (et_yanzheng.getText().toString().equals(yanzhengma)) {
                            Intent intent = new Intent(AppContext.getContext(), SetPassWordAcitvity.class);
                            intent.putExtra("phone", et_phone.getText().toString());
                            startActivity(intent);
                            AppManager.getAppManager().finishActivity();
                            finish();
                        } else {
                            ShowToast("请输入正确的验证码");
                        }
                    }
                }

                break;
            case R.id.btn_send:
                tv_init.setTextColor(getResources().getColor(R.color.text_black));
                tv_shouji.setTextColor(getResources().getColor(R.color.theme_green));
                tv_yanzheng.setTextColor(getResources().getColor(R.color.text_black));
                if (Tools.isMobileNO(et_phone.getText().toString().trim())) {
                    /**
                     * 获取验证码
                     */
                    JSONArray array = new JSONArray();
                    JSONObject object = new JSONObject();
                    try {
                        object.put("phone", et_phone.getText().toString());
                        array.put(object);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    MyNet.doPostJsonArray("107", array, new callBackJsonArray() {
                                @Override
                                public void run(Object code, Object message, JSONArray array) {
                                    if (array == null) {
                                        Log.w("array", array + "");
                                        return;
                                    } else {
                                        try {
                                            JSONObject object1 = array.getJSONObject(0);
                                            Log.w("array", array + "");
                                            yanzhengma = object1.getString("validate_num");
                                            // Toast.makeText(RegisterActivity.this, yanzhengma, Toast.LENGTH_SHORT).show();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                }
                            }

                    );


                    handler.sendEmptyMessage(WHAT_DAOJISHI);

                } else {
                    ShowToast("请输入正确的手机号");
                }


                break;
            default:
                break;
        }
    }
}
